package uiLayer;

import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

public class MachineUI {
    private MachineCtr mcnCtr;
    private Machine mcn;
    
    public MachineUI() {
        mcnCtr = new MachineCtr();
    }
    
    /**
     * Machine menu methods
     */
    public void machinesMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeMachinesMenu();
            
            switch(choice) {
            case 1 : createMachine();
                    stop = true;
                break;
            case 2 : findMachine();
                break;
            case 3 : updateMachine();
                break;
            case 4 : removeMachine();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    }
    
    private int writeMachinesMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Machine Menu ***");
        System.out.println("1. Add machine ");
        System.out.println("2. Find machine ");
        System.out.println("3. Update machine ");
        System.out.println("4. Remove machine ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /**
     * Machine input information
     */
    public int inputBarcode() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the machine (numeric value) : ");
        int serialNumber = keyboard.nextInt();
        return serialNumber;
    }
    
    private String inputName() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name of the machine : ");
        String name = keyboard.nextLine();
        return name;
    }
    
    private double inputPricePerDay() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the price/day : ");
        double price = keyboard.nextInt();
        return price;
    }
  
    /**
     * Machine show information
     */
    private void showName(String name)
    {
        System.out.println("Name : " + name);
    }
    
    private void showPricePerDay(double pricePerDay)
    { 
        System.out.println("Price/Day : " + pricePerDay);
    }
    
    private void showBarcode(int barcode)
    { 
        System.out.println("Barcode : " + barcode);
    }
    
    /**
     * Find machine method 
     **/
     private void findMachine() {
        int barcode = inputBarcode();
        mcn = (mcnCtr.findMachine(barcode));
        if (mcn!= null) {
            System.out.println();
            showName(mcn.getName());
            showPricePerDay(mcn.getPricePerDay());
            showBarcode(mcn.getBarcode());
            System.out.println("Number of available items : " + mcn.getItemsNo());
            System.out.println();
            itemFindMenu();
        }
        else {
            System.out.println("No machine was found with the specified barcode.");
        }
        MainMenuUI.pause();
    }
    
    /**
     * Update machine
     */
    private void updateMachine() {
        Scanner keyboard = new Scanner(System.in);
        int barcode = inputBarcode();
        mcn = (mcnCtr.findMachine(barcode));
        boolean stop = false;
        if (mcn != null) {
            showName(mcn.getName());
            showPricePerDay(mcn.getPricePerDay());
            showBarcode(mcn.getBarcode());
            
            System.out.println();
            System.out.println("1. Update barcode");
            System.out.println("2. Update name");
            System.out.println("3. Update price/day");
            System.out.println("4. Return to previous menu");
            
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {    
                case 1 : mcn.setBarcode(inputBarcode());
                    break;
                case 2 : mcn.setName(inputName());
                    break;
                case 3 : mcn.setPricePerDay(inputPricePerDay());
                    break;
                case 4 : stop = true;
                    break;
                }
                System.out.println();
                System.out.println("The machine information was successfully updated");
            }
        }
        
        else {
            System.out.println("No machine was found with the specified barcode.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    /**
     * Create machine method
     */
    private void createMachine() {
        
        String name = inputName();
        double pricePerDay = inputPricePerDay();
        int barcode = inputBarcode();
        
        mcnCtr.createMachine(name, barcode, pricePerDay);
        System.out.println();
        System.out.println("The machine was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void removeMachine() {
        Scanner keyboard = new Scanner(System.in);
        int barcode = inputBarcode();
        mcn = (mcnCtr.findMachine(barcode));
        boolean stop = false;
        if (mcn != null) {
            System.out.println();
            showName(mcn.getName());
            showPricePerDay(mcn.getPricePerDay());
            showBarcode(mcn.getBarcode());
            System.out.println("Number of available items : " + mcn.getItemsNo());
            
            System.out.println();
            System.out.println("1. Remove machine");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while (!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : mcnCtr.removeMachine(mcn);
                    System.out.println();
                    System.out.println("The machine was successfully removed");
                    System.out.println();
                    MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
                }
            }
        }
        else {
            System.out.println("No machine was found with the specified barcode.");
        }
    }
    
    /**
     * Find item submenu
     */
    private void itemFindMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeItemFindMenu();
            
            switch(choice) {
            case 1 : createItem();
                break;
            case 2 : findItem();
                break;
            case 3 : removeItem();
                break;
            case 0 : stop = true; 
                break;
            default : System.out.println("Invalid command");
                break;
            }
        }
    }
    
    private int writeItemFindMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Find item Submenu ***");
        System.out.println("1. Add item ");
        System.out.println("2. Find item ");
        System.out.println("3. Delete item ");
        System.out.println("0. Return to previous menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    } 
    
    private void createItem() {
        int serialNumber = inputItemSerialNumber();
        
        mcnCtr.createItem(serialNumber);
        System.out.println();
        System.out.println("The item was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void findItem() {
        int serialNumber = inputItemSerialNumber();
        Item it = (mcnCtr.findItem(serialNumber));
        if (it != null) {
            System.out.println();
            System.out.println("Item serial number : " + serialNumber);
        }
        else {
            System.out.println("No item was found with the specified serial number.");
        }
        System.out.println();
        MainMenuUI.pause();
    }
    
    private void removeItem() {
        Scanner keyboard = new Scanner(System.in);
        int serialNumber = inputItemSerialNumber();
        Item it = (mcnCtr.findItem(serialNumber));
        boolean stop = false;
        if (it != null) {
            System.out.println();
            System.out.println("Item number : " + serialNumber);
            
            System.out.println();
            System.out.println("1. Remove item");
            System.out.println("0. Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : mcnCtr.removeItem(it);
                        System.out.println();
                        System.out.println("The item was successfully removed");
                        mcn.setItemsNo(mcn.getItemsNo()-1);
                        System.out.println();
                        MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
               }
            }
        }
        else {
            System.out.println("No item was found with the specified serial number.");
        }
    }
    
    public int inputItemSerialNumber() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the serial number of the item (numeric value) : ");
        int serialNumber = keyboard.nextInt();
        return serialNumber;
    }
            
}
